package com.bird.config

import com.bird.component.exception.GlobalExceptionHandler
import com.bird.component.jackson.MeditateJackson2ObjectMapperBuilderCustomizer
import com.bird.component.log.WebLogAspect
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.boot.autoconfigure.jackson.JacksonProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.EnableAspectJAutoProxy
import org.springframework.context.annotation.Import
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder

@Configuration
@EnableConfigurationProperties(JacksonProperties::class)
@EnableAspectJAutoProxy
@Import(WebLogAspect::class)
class MeditateDefaultConfiguration() {

    @Bean
    fun jackson2ObjectMapperBuilder(
        applicationContext: ApplicationContext,
        jacksonProperties: JacksonProperties
    ): Jackson2ObjectMapperBuilder {
        val builder = Jackson2ObjectMapperBuilder.json()
        MeditateJackson2ObjectMapperBuilderCustomizer(applicationContext, jacksonProperties).customize(builder)
        builder.findModulesViaServiceLoader(true)
        return builder
    }
    @Bean
    fun jacksonObjectMapper(applicationContext: ApplicationContext, jacksonProperties: JacksonProperties):ObjectMapper {
        return jackson2ObjectMapperBuilder(applicationContext, jacksonProperties).build()
    }

    @Bean
    fun globalExceptionHandler(): GlobalExceptionHandler {
        return GlobalExceptionHandler()
    }

}